﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Data.Car_Rental;
using Entity.Car_Rental;
namespace Background.Controllers
{
    public class ShopController : Common.Background.Web.Controllers.BackgroundBaseController
    {

        IShopRepository iShopRepository = null;
        public ShopController()
        {
            iShopRepository = new ShopRepository();
        }
        //
        // GET: /Shop/

        public ActionResult Index(string Province, string City, string District, int? page)
        {
            VP = new Entity.VPredication();
            PP = new Entity.PagingParam(page ?? 1, base.PageSize);
            string area = null;
            int length = 2;
            if (!string.IsNullOrEmpty(Province) && string.IsNullOrEmpty(City) && string.IsNullOrEmpty(District))
            {
                area = Province;
                length = 2;
            }
            if (!string.IsNullOrEmpty(City) && string.IsNullOrEmpty(District))
            {
                area = City;
                length = 4;
            }
            if (!string.IsNullOrEmpty(District))
            {
                area = District;
                length = 6;
            }
            if (!string.IsNullOrEmpty(area))
                VP.AddItem("area", area);

            VP.AddItem("length", length);
            var model = iShopRepository.GetShopByAreaID(VP, PP);
            if (Request.IsAjaxRequest())
                return PartialView("List", model);
            else
                return View(model);
        }

        //
        // GET: /Shop/Details/5

        public ActionResult Details(int? id)
        {
            if (id.HasValue)
            {
                var model = iShopRepository.Find(id.Value);
                return View(model);
            }
            else
                return View();
        }

        [HttpPost]
        public ActionResult Details(FormCollection form)
        {

            Shop entity = new Shop();
            TryUpdateModel(entity);
            int areaID = -1;
            if (form["Province"] != "-1" && form["City"] == "-1" && form["District"] == "-1")
                int.TryParse(form["Province"], out areaID);
            if (form["City"] != "-1" && form["District"] == "-1")
                int.TryParse(form["City"], out areaID);
            if (form["District"] != "-1")
                int.TryParse(form["District"], out areaID);
            entity.AreaID = areaID;
            entity.CreateDate = DateTime.Now;
            entity.UpdateDate = DateTime.Now;
            entity.Status = (int)Entity.CommonEnums.Status.Normal;
            if (entity.IsValid)
            {
                if (entity.ShopID == 0)
                    iShopRepository.Insert(entity);
                else
                    iShopRepository.Update(entity);
                return AlertToUrl("Index");
            }
            else
            {
                entity.GetRuleViolations().ToList().ForEach(i =>
                {
                    ModelState.AddModelError(i.PropertyName, i.ErrorMessage);
                });
                return View(iShopRepository.Find(entity.ShopID));
            }

        }
        //
        // GET: /Shop/Delete/5

        public ActionResult Delete(int id)
        {
            iShopRepository.Delete(new Shop { ShopID = id });
            return AlertToUrl("Index");
        }

    }
}
